using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 记录敌人的可掉落物
/// </summary>
public class LootSpawner : MonoBehaviour
{
    [System.Serializable]
    public class LootItem
    {
        public GameObject item;
        [Header("权重")]
        [Range(0, 1)]
        public float weight;
    }
    public LootItem[] lootItems;
    public void SpawnLoot()
    {
        float currentValue = Random.value;
        // 掉落物品
        for (int i = 0; i < lootItems.Length; i++)
        {
            if (currentValue <= lootItems[i].weight)
            {
                GameObject obj = Instantiate(lootItems[i].item);
                
                // 在头顶一定距离生成
                obj.transform.position = transform.position + Vector3.up * 3;
                if (obj.GetComponent<Rigidbody>())
                {
                    obj.GetComponent<Rigidbody>().velocity = Vector3.up;
                }

                // 每次掉落后 50% 几率不再掉落后续物品
                if (Random.value < 0.5f)
                {
                    break;
                }
            }
        }
    }
}
